package com.lf.redis;

import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings("unchecked")
public class RedisServiceImpl implements IRedisService {
    @Resource
    private StringRedisTemplate rt;

    private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newScheduledThreadPool(10);

    private static final String DELIMITER = "|";

    /**
     * @param key the key
     * @return
     */
    @Override
    public String getString(String key) {
        String value = null;
        ValueOperations<String, String> ops = rt.opsForValue();
        if (rt.hasKey(key)) {
            value = ops.get(key);
        }
        return value;
    }

    /**
     * @param key the key
     */
    @Override
    public void deleteString(String key) {
        rt.delete(key);
    }

    @Override
    public void setString(String key, String value) {
        Preconditions.checkArgument(StringUtils.isNotEmpty(key), "Redis key is not null");

        ValueOperations<String, String> ops = rt.opsForValue();
        ops.set(key, value);

    }

    @Override
    public void setString(String key, String value, long timeout, TimeUnit unit) {
        Preconditions.checkArgument(StringUtils.isNotEmpty(key), "Redis key is not null");
        Preconditions.checkArgument(unit != null, "TimeUnit is not null");
        ValueOperations<String, String> ops = rt.opsForValue();
        try {
            ops.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        rt.expire(key, timeout, unit);
    }

    /**
     * 设置键过期时间
     *
     * @param key
     * @param timeout
     * @param unit
     */
    public void expireKey(String key, final long timeout, final TimeUnit unit) {
        Preconditions.checkArgument(StringUtils.isNotEmpty(key), "Redis key is not null");
        Preconditions.checkArgument(unit != null, "TimeUnit is not null");
        rt.expire(key, timeout, unit);
    }

    /**
     * 是否存在key
     *
     * @param key
     * @return
     */
    @Override
    public boolean hasKey(String key) {
        return rt.hasKey(key);
    }

    /**
     * @param key the key
     * @return
     */
    @Override
    public Set<String> keys(String key) {
        Set<String> keys = rt.keys(key+ "*");
        if(keys != null && keys.size() != 0) {
            return keys;
        }
        return null;
    }

    @Override
    public void increment(String key) {
        if(hasKey(key)){
           rt.boundValueOps(key).increment(1);
        }
    }

    @Override
    public void decrease(String key){
        if(hasKey(key)){
            rt.boundValueOps(key).increment(-1);
        }
    }
}
